<?php
/** Cms_Resource_Payroll_Item */
if (!class_exists('Cms_Resource_Payroll_Item')) {
    require_once dirname(__FILE__) . '/Payroll/Item.php';
}

/**
 * Cms_Resource_Payroll
 *
 * @category   Cms
 * @package    Cms_Model_Resource
 */
class Cms_Resource_Payroll extends BIS_Model_Resource_Db_Table_Abstract implements Cms_Resource_Payroll_Interface
{
    protected $_name = 'payroll';
    protected $_primary = 'entryId';
    protected $_rowClass = 'Cms_Resource_Payroll_Item';

    public function getPayrollByEntryId($id)
    {
        return $this->find($id)->current();
    }
    

    public function getPayrollByStaffId($staffId)
    {
    	$select = $this->select()->where('staffId = ?', $staffId);
    	return $this->fetchAll($select);
    }
    
    public function getPayrolls($data= array())
    {
        
        $select = $this->select();

        $select->from(array('p'=>'payroll'),
        		array('name'=>'s.name', 'p.*')
        )
        ->setIntegrityCheck(false)
        ->join(array('s'=>'staff'), 'p.staffId = s.staffId', array('s.staffId'));
        
        if (isset($data['search'])) {
        	$search = $data['search'];
        	$select->where("s.name like ? OR p.staffPosition like ?", "%$search%");
        }
        
        if (isset($data['order'])) {
            $select->order($data['order']);
        }

	if (isset($data['page'])) {
		$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage($data['limit'])
	          	  ->setCurrentPageNumber((int) $data['page']);
		return $paginator;
	}
        
        return $this->fetchAll($select);

    }
   
    public function getPayrollReport($fromDate=null, $toDate=null){
    	$select = $this->select ();
    	
    	$select->from(array('p'=>'payroll'),
    			array('name'=>'s.name', 'p.*')
    	)
    	->setIntegrityCheck(false)
    	->join(array('s'=>'staff'), 'p.staffId = s.staffId', array('s.staffId'));
    	
    	if (!empty($fromDate)) {
    		$toDate = (!empty($toDate)) ? $toDate : date('Y-m-d');
    		$select->where("date(p.datePaid) between '$fromDate' and '$toDate'");
    	}
    	
    	$select->order ('p.datePaid DESC');
    	
    	return $this->fetchAll ( $select );
    }
    
}